﻿using System;
using System.Collections.Generic;
using System.Linq;
using DLM.Domain.Model;
using DLM.Domain.Repositories;
using DLM.Infrastructure.UnitOfWork;
using DLM.Infrastructure.Domain;
using Webdiyer.WebControls.Mvc;

namespace DLM.Data.EF.CodeFirst.Repositories
{
    public class MessageRepository : Repository<Message, int>, IMessageRepository
    {
        public MessageRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        { }

        public IEnumerable<Message> FindByUser(string accountName,int pageIndex,int pageSize)
        {
            if (string.IsNullOrEmpty(accountName))
            {
                throw new ArgumentNullException("Account name");
            }
            var result = from a in this.ActiveContext.Account
                         join u in this.ActiveContext.User
                         on a.AccountID equals u.AccountID
                         join m in this.ActiveContext.Message
                         on u.UserID equals m.UserID
                         where a.AccountName == accountName
                         && m.IsRead == false
                         select m;
            return result.OrderByDescending(m => m.CreateDate).ToPagedList(pageIndex, pageSize);

        }
    }
}
